﻿using System.Data;
using System.Text;
using System.Web;
using Utility;

namespace Thksoft.UserCotrols
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //  context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string command = context.Request.QueryString["Action"];//前台传的标示值

            if (command == "select")
            {
                //查询
                Query(context);
            }
            else if (command == "modify")
            {//调用修改方法
                //Modify(context);//我暂时只是绑定，所以把这些给注释了
            }
            else
            {//调用查询方法
                Query(context);
            }
        }

        public void Query(HttpContext context)
        {
            //调用B层的方法从而获取数据库的Dataset
            // SqlHelp sqla = new SqlHelp();//这个是我自己写的一个sqlhelp类，其实sql执行语句，网上有很多这样的类
            string strfaca = @"SELECT lcq.N_QUEUE_ID,
     lcp.C_PROCESS_NAME，
       lcq.N_QUEUE_NUMBER
  FROM Lc_Queue lcq, Lc_Process lcp, Lc_Node lcn
 WHERE lcq.N_PROCESS_ID = lcp.N_PROCESS_ID
   AND lcq.N_NODE_ID = lcn.N_NODE_ID";//这里根据你自己的情况修改就行了
            DataSet ds = DbHelperSQL.Query(strfaca);// new DbHelperSQL().Query(strfaca); //sqla.GetDataSet(strfaca);
            //将Dataset转化为Datable
            DataTable dt = ds.Tables[0];
            int count = dt.Rows.Count;
            string strJson = DatasetToJson(ds, count);//DataSet数据转化为Json数据
            context.Response.Write(strJson);//返回给前台页面
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// DataSet转换成Json格式
        /// </summary>
        /// <paramname="ds">DataSet</param>
        ///<returns></returns>
        public string DatasetToJson(DataSet ds, int total = -1)
        {
            StringBuilder json = new StringBuilder();

            foreach (DataTable dt in ds.Tables)
            {
                //{"total":5,"rows":[
                json.Append("{\"total\":");
                if (total == -1)
                {
                    json.Append(dt.Rows.Count);
                }
                else
                {
                    json.Append(total);
                }
                json.Append(",\"rows\":[");
                json.Append(DataTableToJson(dt));
                json.Append("]}");
            }
            return json.ToString();
        }

        /// <summary>
        /// dataTable转换成Json格式
        /// </summary>
        /// <paramname="dt"></param>
        ///<returns></returns>
        public string DataTableToJson(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                if (dt.Columns.Count > 0)
                {
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                }
                jsonBuilder.Append("},");
            }
            if (dt.Rows.Count > 0)
            {
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            }

            return jsonBuilder.ToString();
        }
    }
}